package com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.entity.IECustomsDeclarationAttDocFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository("singleWindow.ieCustomsDeclarationAttDocFileDao")
public class IECustomsDeclarationAttDocFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String existsCustomsDeclarationFileSql = "SELECT count(1) FROM gd_jckhwgd_att_doc_file WHERE attEdocId = ?";

    public boolean exists(String attEdocId) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsCustomsDeclarationFileSql, new Object[]{attEdocId}, Integer.class);
        return count != null && count > 0;
    }

    private String findAttEdocIdInSql = "SELECT attEdocId FROM gd_jckhwgd_att_doc_file WHERE attEdocId in (:attEdocIds)";


    public Map<String, Boolean> batchCheckExists(List<String> attEdocIds) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("attEdocIds", attEdocIds);
        List<String> existingIds = namedParameterJdbcTemplate.query(this.findAttEdocIdInSql, params, (rs, rowNo) -> rs.getString("attEdocId"));

        HashSet<String> existingSet = new HashSet<>(existingIds);
        HashMap<String,  Boolean> result = new HashMap<>();
        for (String attEdocId : attEdocIds) {
            result.put(attEdocId, existingSet.contains(attEdocId));
        }
        return result;
    }

    private String selectCustomsDeclarationFileSql = "SELECT file FROM gd_jckhwgd_att_doc_file WHERE attEdocId = ?";

    public String findByAttEdocId(String attEdocId) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectCustomsDeclarationFileSql,
                new Object[]{attEdocId}, String.class);
    }

    private String insertCustomsDeclarationRetFileSql = "INSERT INTO gd_jckhwgd_att_doc_file (attEdocId, file, storageTime) VALUES (:attEdocId, :file, :storageTime)";

    public void save(IECustomsDeclarationAttDocFile customsDeclarationAttDocFile) {
        customsDeclarationAttDocFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertCustomsDeclarationRetFileSql, new BeanPropertySqlParameterSource(customsDeclarationAttDocFile));
    }

    private String deleteCustomsDeclarationRetFileSql = "DELETE FROM gd_jckhwgd_att_doc_file WHERE attEdocId = ?";

    public void delete(String attEdocId) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteCustomsDeclarationRetFileSql, attEdocId);
    }
}
